"""
十二、从多个网页中获取信息，组合成一个item

1. 爬取页面 https://stackoverflow.com/?tab=month 中所有的question的以下信息：

    1. 标题title
    2. 标签tags
    3. 修改时间modify time
    4. 投票votes
    5. 回复answers
    6. 查看views
    7. 问题的详细文字(xpath: //div[@class="post-text"]//*/text())
"""

import scrapy


class StackSpider(scrapy.Spider):
    name = 'stack'
    start_urls = ['https://stackoverflow.com/?tab=month']

    def parse(self, response):
        for div in response.css('div.question-summary'):
            title = div.css('div.summary h3 a::text').extract_first()
            tags = div.css('div.summary .tags a::text').extract()
            mtime = div.css('div.summary .relativetime::attr(title)').extract_first()
            many = div.css('.cp div.mini-counts span::attr(title)').extract()
            vote, answer, view = [int(x.split()[0]) for x in many]
            item = dict(title=title, tags=tags, mtime=mtime, vote=vote,
                        answer=answer, view=view)
            anchor = div.css('div.summary h3 a')[0]
            yield response.follow(anchor, meta={'item': item}, callback=self.get_detail)

    def get_detail(self, response):
        item = response.meta['item']
        # texts = response.xpath('//div[@class="post-text"]//*/text()').extract()
        texts = response.css('div.post-text')[0].css('*::text').extract()
        detail = ''.join(texts)
        item['detail'] = detail
        yield item
